package com.atguigu.acl.controller;


import com.atguigu.acl.dto.PermissionNode;
import com.atguigu.acl.entity.Permission;
import com.atguigu.acl.service.PermissionService;
import com.atguigu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-08-10
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    @ApiOperation("添加权限")
    @PostMapping(value = "/create")
    public Result create(@RequestBody Permission permission) {
        int count = permissionService.create(permission);
        if(count>0){
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("修改权限")
    @PostMapping(value = "/update/{id}")
    public Result update(@PathVariable Long id, @RequestBody Permission permission) {
        int count = permissionService.update(id,permission);
        if(count>0){
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("根据id批量删除权限")
    @PostMapping(value = "/delete")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        int count = permissionService.delete(ids);
        if(count>0){
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("以层级结构返回所有权限")
    @GetMapping(value = "/treeList")
    public Result treeList() {
        List<PermissionNode> permissionNodeList = permissionService.treeList();
        return Result.ok().data("permissionNodeList",permissionNodeList);
    }

    @ApiOperation("获取所有权限列表")
    @GetMapping("/list")
    public Result list() {
        List<Permission> permissionList = permissionService.list();
        return Result.ok().data("permissionList",permissionList);
    }
}

